<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\common\model;

use think\Model as ThinkModel;

//银行列表模型
class ViperActivityGoods extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $table = '__QT_VIPER_ACTIVITY_GOODS__';
    protected $name = 'qt_viper_activity_goods';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    public static function createByTemplate($arr,$store_id,$viper_type_id)
    {
        foreach ($arr as $item)
        {
            if(!isset($item['type']))$item['type'] = null;
            if(!isset($item['value']))$item['value'] = null;
            if(!isset($item['start']))$item['start'] = null;
            if(!isset($item['end']))$item['end'] = null;
            if(!isset($item['rang_viper_price']))$item['rang_viper_price'] = null;
            if(!isset($item['rule']))$item['rule'] = null;
            ViperActivityGoods::create([
                'product_id' => $item['id'],
                'type' => $item['type'],
                'value' =>$item['value'],
                'start' => $item['start'],
                'end' => $item['end'],
                'viper_price' => $item['viper_price'],
                'rang_viper_price' => $item['rang_viper_price'],
                'rule' => $item['rule'],
                'viper_type_id' => $viper_type_id,
                'store_id' =>$store_id
            ]);
        }
    }

    public static function getActivityGoodsOfStore($store_id,$viper_type_id)
    {
        $list = self::alias('a')
            ->leftJoin('dp_qt_goods g','a.product_id=g.id')
        ->where(
            ['a.store_id'=>$store_id,'a.viper_type_id'=>$viper_type_id])
            ->field('a.product_id id,a.type,a.value,a.start,a.end,a.viper_price,a.rang_viper_price,a.rule,g.name,g.price,a.id as ac_id')
            ->select();
        //删除无效的商品
        foreach ($list as $key=>$value){
            if(empty($value['name'])){
                self::where(['id'=>$value['ac_id']])->delete();
            }
        }

        return $list;
    }
}